(function () {
    var $,
        addBook = function () {},
        editBook = function () {},
        removeBook = function () {};

    function Bookshelf(rootElem, jQuery) {
        $ = jQuery;

        this.shelf      = $('.shelf-surface', rootElem);
        this.collection = $('.book-collection', this.shelf);
        this.books      = $('.books li', this.collection);
        this.addButton  = $('.add-new a', this.collection);

        this.addButton.click(function (event) {
            event.preventDefault();
            addBook();
        });

        this.books.live('click', function (event) {
            var triggeredElemTag = event.target.tagName.toString().toLowerCase(),
                postID = $(this).attr('data-book-id');

            if (triggeredElemTag == 'span' && $(event.target).hasClass('close')) {
                // remove this book
                removeBook($(this), postID);
            } else {
                // edit action is the default
                editBook(postID);
            }
        });
    };

    Bookshelf.prototype.hide = function () {
        this.shelf.hide();
    }

    Bookshelf.prototype.show = function () {
        this.shelf.fadeIn(300);
    }

    Bookshelf.prototype.on = function (event, callback) {
        switch (event) {
            case 'addBook':
                addBook = callback;
                break;
            case 'editBook':
                editBook = callback;
                break;
            case 'removeBook':
                removeBook = callback;
                break;
            default:
                break
        }
    }

    Bookshelf.prototype.update = function (items) {
        $('.books', this.collection).html(items);
    }

    window.Bookshelf = Bookshelf;
}());

